package equipments;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import systems.database.DataBaseHandler;
import systems.languages.MultiLanguages;
import tools.BonusAttribut;
import tools.Visitable;
import tools.Visitor;


public class Manastone implements Items,Visitable {

  private enum bonusType{
    maxhp,
    maxmp,
    hitaccuracy,
    dodge,
    phyattack,
    magicalskillboost,
    parry,
    block,
    critical,
    magicalhitaccuracy,
    magicalresist
  };
  private String name;
  private int id = 0;
  private int level;
  private BonusAttribut bAttribut;

  public Manastone(){
  }
  
  @Override
  public void init(String id) {
    this.id = Integer.valueOf(id);
    this.initItem(DataBaseHandler.getStatement());
  }
  @Override
  public int getLevel() {
    return this.level;
  }

  @Override
  public String getName() {
    return this.name;
  }

  @Override
  public int getNbManastone() {
    return 0;
  }

  @Override
  public boolean hasDivineSlot() {
    return false;
  }

  public BonusAttribut getBonus()
  {
    return this.bAttribut;
  }

  public int getID()
  {
    return this.id;
  }

  @Override
  public void initItem(Statement transmission) {
    ResultSet resultat;
    String stattoEnchant="";
    int bonus=0;
    try {

      //Recuperation de l'id de l'arme
      resultat= transmission.executeQuery("Select oid from strings_fr where body like '"+this.name+"' and oid > 100000000");
      while(resultat.next())
        this.id=resultat.getInt("oid");
      //Recuperation des stats des initialisation
      resultat = transmission.executeQuery("Select * from items where id="+id);
      while(resultat.next())
      {
        bonus = resultat.getInt("stat_enchant_value");
        stattoEnchant =resultat.getString("stat_enchant_type");
        this.level = resultat.getInt("level");
      }

    } catch (SQLException e) {
      e.printStackTrace();
    }
    this.bAttribut= new BonusAttribut(bonusType.valueOf(stattoEnchant.toLowerCase()).toString(), bonus);
    //System.out.println(this.toString());
  }

  public String toString()
  {
    return "Name :"+ this.name+
    "\nlevel: "+this.level+
    this.bAttribut.toString();
  }

  @Override
  public int getNbBonusAttr() {
    return 0;
  }

  @Override
  public void accept(Visitor visitor) {
    visitor.visit(this);
  }

  public void updateName() {
    ResultSet resultat;
    try {
      resultat= DataBaseHandler.getStatement().executeQuery("SELECT body FROM "
          +MultiLanguages.getProperties("database.Language.name")
          +" where oid = "+this.id);
      while(resultat.next())
        this.name=resultat.getString("body");  
    } catch (SQLException e) {
      e.printStackTrace();
    }
  }
  @Override
  public void acceptBonusRemoval(Visitor visitor) {
    visitor.bonusvisit(this);		
  }
}
